/*
Miranda IM History Sweeper plugin
Copyright (C) 2002-2003  Sergey V. Gershovich
Copyright (C) 2006-2007  borkra

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "historysweeper.h"

static int bListInit = 0; 
static HANDLE hItemAll, hItemSystem;

// Menu items
const char* menu_items[3] = 
{
	"Sweep system history",
	"Sweep whole history",
	"Sweep history from selected contacts"
};

// Time Stamps strings
static const char* time_stamp_strings[9] = 
{
	"All events", "Older than 1 Day", 
	"Older than 3 Days", "Older than 7 Days", "Older than 2 Weeks (14 days)",
	"Older than 1 Month (30 days)", "Older than 3 Months (90 days)",
	"Older than 6 Months (180 days)","Older than 1 Year (364 days)"
};

void ResetListOptions(HWND hwndList)
{
	int i;

	SendMessage(hwndList,CLM_SETBKBITMAP,0,(LPARAM)(HBITMAP)NULL);
	SendMessage(hwndList,CLM_SETBKCOLOR,GetSysColor(COLOR_WINDOW),0);
	SendMessage(hwndList,CLM_SETGREYOUTFLAGS,0,0);
	SendMessage(hwndList,CLM_SETLEFTMARGIN,4,0);
	SendMessage(hwndList,CLM_SETINDENT,10,0);

	for(i=0;i<=FONTID_MAX;i++)
		SendMessage(hwndList,CLM_SETTEXTCOLOR,i,GetSysColor(COLOR_WINDOWTEXT));
}

// Selects the "All contacts" checkbox if all other list entries
// are selected, deselects it if not.
// taken from icqj sources
static void UpdateAllContactsCheckmark(HWND hClist, HANDLE phItemAll)
{
  int check = 1;
  HANDLE hContact;
  HANDLE hItem;

  hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST,0,0);
  while (hContact)
  {
    hItem = (HANDLE)SendMessage(hClist, CLM_FINDCONTACT, (WPARAM)hContact, 0);
    if (hItem)
    {
      if (!SendMessage(hClist, CLM_GETCHECKMARK, (WPARAM)hItem, 0))
      { // if any of our contacts is unchecked, uncheck all contacts as well
        check = 0;
        break;
      }
    }
    hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact,0);
  }

  if (!SendMessage(hClist, CLM_GETCHECKMARK, (WPARAM)hItemSystem, 0))
  { // if System history is unchecked, uncheck all contacts as well 
	  check = 0;
  }
  
  SendMessage(hClist, CLM_SETCHECKMARK, (WPARAM)phItemAll, check);
}

void SetAllNoneEventsCheckmarks(HWND hwndDlg, UINT check)
{
	CheckDlgButton(hwndDlg,IDC_EVENT_INCOMINGMESSAGES,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_OUTMESSAGES,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_URL,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUSMESSAGE,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_FILES,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_CONTACTS,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUS,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_NICK,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_AVATAR,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_WATRACK,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_VOICECALL,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQREMOVE,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQIGNORECHECK,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCLIENTCHANGE,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQAUTH,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCHECKSTATUS,check);
	CheckDlgButton(hwndDlg,IDC_EVENT_UNKNOWN,check);
};

void LoadSettingsGeneral(HWND hwndDlg)
{
	int i;
	for(i=0;i<=2;i++) {
		SendDlgItemMessage(hwndDlg,IDC_MMITEM,CB_ADDSTRING,0,(LPARAM)Translate(menu_items[i]));
		SendDlgItemMessage(hwndDlg,IDC_ACTION,CB_ADDSTRING,0,(LPARAM)Translate(menu_items[i]));
	}
	for(i=0;i<=8;i++) {
        SendDlgItemMessage(hwndDlg,IDC_MMOLDER,CB_ADDSTRING,0,(LPARAM)Translate(time_stamp_strings[i]));
		SendDlgItemMessage(hwndDlg,IDC_SSOLDER,CB_ADDSTRING,0,(LPARAM)Translate(time_stamp_strings[i]));
	}
	
    SendDlgItemMessage(hwndDlg,IDC_MMITEM,CB_SETCURSEL,DBGetContactSettingByte(0,ModuleName,"MainMenuAction",0),0);
    SendDlgItemMessage(hwndDlg,IDC_ACTION,CB_SETCURSEL,DBGetContactSettingByte(0,ModuleName,"StartupShutdownAction",0),0);
	
	SendDlgItemMessage(hwndDlg,IDC_MMOLDER,CB_SETCURSEL,DBGetContactSettingByte(0,ModuleName,"MainMenuOlder",0),0);
	SendDlgItemMessage(hwndDlg,IDC_SSOLDER,CB_SETCURSEL,DBGetContactSettingByte(0,ModuleName,"StartupShutdownOlder",0),0);
	
	CheckDlgButton(hwndDlg,IDC_STARTUPSWEEP,DBGetContactSettingByte(0,ModuleName,"StartupSweep",0));
	CheckDlgButton(hwndDlg,IDC_SHUTDOWNSWEEP,DBGetContactSettingByte(0,ModuleName,"ShutdownSweep",0));
	CheckDlgButton(hwndDlg,IDC_MAINMENUICON,DBGetContactSettingByte(0,ModuleName,"MainMenuIcon",1));
	CheckDlgButton(hwndDlg,IDC_CONTACTMENUICON,DBGetContactSettingByte(0,ModuleName,"ContactMenuIcon",1));
	
	CheckDlgButton(hwndDlg,IDC_UNSAFEMODE,DBGetContactSettingByte(0,ModuleName,"UnsafeMode",0));

	CheckDlgButton(hwndDlg,IDC_EVENT_INCOMINGMESSAGES,DBGetContactSettingByte(0,ModuleName,"EventsIncomingMessages",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_OUTMESSAGES,DBGetContactSettingByte(0,ModuleName,"EventsOutgouingMessages",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_URL,DBGetContactSettingByte(0,ModuleName,"EventsURL",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUSMESSAGE,DBGetContactSettingByte(0,ModuleName,"EventsStatusMessage",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_FILES,DBGetContactSettingByte(0,ModuleName,"EventsFiles",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_CONTACTS,DBGetContactSettingByte(0,ModuleName,"EventsContacts",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUS,DBGetContactSettingByte(0,ModuleName,"EventsStatus",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_NICK,DBGetContactSettingByte(0,ModuleName,"EventsNick",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_AVATAR,DBGetContactSettingByte(0,ModuleName,"EventsAvatar",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_WATRACK,DBGetContactSettingByte(0,ModuleName,"EventsWATrack",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_VOICECALL,DBGetContactSettingByte(0,ModuleName,"EventsVoiceCall",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQREMOVE,DBGetContactSettingByte(0,ModuleName,"EventsICQRemove",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQIGNORECHECK,DBGetContactSettingByte(0,ModuleName,"EventsICQIgnoreCheck",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCLIENTCHANGE,DBGetContactSettingByte(0,ModuleName,"EventsICQClientChange",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQAUTH,DBGetContactSettingByte(0,ModuleName,"EventsICQAuth",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCHECKSTATUS,DBGetContactSettingByte(0,ModuleName,"EventsICQCheckStatus",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_UNKNOWN,DBGetContactSettingByte(0,ModuleName,"EventsUnknown",0));
	CheckDlgButton(hwndDlg,IDC_EVENT_KEEPBOOKMARKS,DBGetContactSettingByte(0,ModuleName,"KeepHppBookmarks",1));
	
	CheckDlgButton(hwndDlg,IDC_EVENT_INCOMINGMESSAGES,hs_events.EventsIncomingMessages);
	CheckDlgButton(hwndDlg,IDC_EVENT_OUTMESSAGES,hs_events.EventsOutgouingMessages);
	CheckDlgButton(hwndDlg,IDC_EVENT_URL,hs_events.EventsURL);
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUSMESSAGE,hs_events.EventsStatusMessage);
	CheckDlgButton(hwndDlg,IDC_EVENT_FILES,hs_events.EventsFiles);
	CheckDlgButton(hwndDlg,IDC_EVENT_CONTACTS,hs_events.EventsContacts);
	CheckDlgButton(hwndDlg,IDC_EVENT_STATUS,hs_events.EventsStatus);
	CheckDlgButton(hwndDlg,IDC_EVENT_NICK,hs_events.EventsNick);
	CheckDlgButton(hwndDlg,IDC_EVENT_AVATAR,hs_events.EventsAvatar);
	CheckDlgButton(hwndDlg,IDC_EVENT_WATRACK,hs_events.EventsWATrack);
	CheckDlgButton(hwndDlg,IDC_EVENT_VOICECALL,hs_events.EventsVoiceCall);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQREMOVE,hs_events.EventsICQRemove);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQIGNORECHECK,hs_events.EventsICQIgnoreCheck);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCLIENTCHANGE,hs_events.EventsICQClientChange);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQAUTH,hs_events.EventsICQAuth);
	CheckDlgButton(hwndDlg,IDC_EVENT_ICQCHECKSTATUS,hs_events.EventsICQCheckStatus);
	CheckDlgButton(hwndDlg,IDC_EVENT_UNKNOWN,hs_events.EventUnknown);
	CheckDlgButton(hwndDlg,IDC_EVENT_KEEPBOOKMARKS,hs_events.KeepHppBookMarks);
/*
	hs_events.EventsIncomingMessages	= IsDlgButtonChecked(hwndDlg,IDC_EVENT_INCOMINGMESSAGES);
	hs_events.EventsOutgouingMessages	= IsDlgButtonChecked(hwndDlg,IDC_EVENT_OUTMESSAGES);
	hs_events.EventsURL					= IsDlgButtonChecked(hwndDlg,IDC_EVENT_URL);
	hs_events.EventsStatusMessage		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_STATUSMESSAGE);
	hs_events.EventsFiles				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_FILES);
	hs_events.EventsContacts			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_CONTACTS);
	hs_events.EventsStatus				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_STATUS);
	hs_events.EventsNick				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_NICK);
	hs_events.EventsAvatar				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_AVATAR);
	hs_events.EventsWATrack				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_WATRACK);
	hs_events.EventsVoiceCall			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_VOICECALL);
	hs_events.EventsICQRemove			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQREMOVE);
	hs_events.EventsICQIgnoreCheck		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQIGNORECHECK);
	hs_events.EventsICQClientChange		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQCLIENTCHANGE);
	hs_events.EventsICQAuth				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQAUTH);
	hs_events.EventsICQCheckStatus		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQCHECKSTATUS);
	hs_events.EventUnknown				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_UNKNOWN);
	hs_events.KeepHppBookMarks			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_KEEPBOOKMARKS);
*/
}

void LoadSettingsList(HWND hwndDlg)
{
	HWND hClist = GetDlgItem(hwndDlg, IDC_LIST);

	HANDLE hContact=(HANDLE)CallService(MS_DB_CONTACT_FINDFIRST,0,0);
	while ( hContact != NULL )
	{
		if (DBGetContactSettingByte(hContact,"CList","SweepHistory",0))
		{
			HANDLE hItem = (HANDLE)SendMessage(hClist, CLM_FINDCONTACT, (WPARAM)hContact, 0);
			SendMessage(hClist, CLM_SETCHECKMARK, (WPARAM)hItem, 1);
		}

		hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact,0);
	} 

	SendMessage(hClist, CLM_SETCHECKMARK, (WPARAM)hItemSystem, 
		DBGetContactSettingByte(0,ModuleName,"SweepSystemHistory",0));

	UpdateAllContactsCheckmark(hClist, hItemAll);
}

void SaveSettingsGeneral(HWND hwndDlg)
{
	DBWriteContactSettingByte(NULL,ModuleName,"StartupSweep",(BYTE)IsDlgButtonChecked(hwndDlg,IDC_STARTUPSWEEP));
	DBWriteContactSettingByte(NULL,ModuleName,"ShutdownSweep",(BYTE)IsDlgButtonChecked(hwndDlg,IDC_SHUTDOWNSWEEP));
	DBWriteContactSettingByte(NULL,ModuleName,"MainMenuIcon",(BYTE)IsDlgButtonChecked(hwndDlg,IDC_MAINMENUICON));
	DBWriteContactSettingByte(NULL,ModuleName,"ContactMenuIcon",(BYTE)IsDlgButtonChecked(hwndDlg,IDC_CONTACTMENUICON));

	DBWriteContactSettingByte(NULL,ModuleName,"MainMenuAction",(BYTE)SendDlgItemMessage(hwndDlg,IDC_MMITEM,CB_GETCURSEL,0,0));
	DBWriteContactSettingByte(NULL,ModuleName,"StartupShutdownAction",(BYTE)SendDlgItemMessage(hwndDlg,IDC_ACTION,CB_GETCURSEL,0,0));
	DBWriteContactSettingByte(NULL,ModuleName,"MainMenuOlder",(BYTE)SendDlgItemMessage(hwndDlg,IDC_MMOLDER,CB_GETCURSEL,0,0));
	DBWriteContactSettingByte(NULL,ModuleName,"StartupShutdownOlder",(BYTE)SendDlgItemMessage(hwndDlg,IDC_SSOLDER,CB_GETCURSEL,0,0));
	DBWriteContactSettingByte(NULL,ModuleName,"UnsafeMode",(BYTE)IsDlgButtonChecked(hwndDlg,IDC_UNSAFEMODE));
	
	hs_events.EventsIncomingMessages	= IsDlgButtonChecked(hwndDlg,IDC_EVENT_INCOMINGMESSAGES);
	hs_events.EventsOutgouingMessages	= IsDlgButtonChecked(hwndDlg,IDC_EVENT_OUTMESSAGES);
	hs_events.EventsURL					= IsDlgButtonChecked(hwndDlg,IDC_EVENT_URL);
	hs_events.EventsStatusMessage		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_STATUSMESSAGE);
	hs_events.EventsFiles				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_FILES);
	hs_events.EventsContacts			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_CONTACTS);
	hs_events.EventsStatus				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_STATUS);
	hs_events.EventsNick				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_NICK);
	hs_events.EventsAvatar				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_AVATAR);
	hs_events.EventsWATrack				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_WATRACK);
	hs_events.EventsVoiceCall			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_VOICECALL);
	hs_events.EventsICQRemove			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQREMOVE);
	hs_events.EventsICQIgnoreCheck		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQIGNORECHECK);
	hs_events.EventsICQClientChange		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQCLIENTCHANGE);
	hs_events.EventsICQAuth				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQAUTH);
	hs_events.EventsICQCheckStatus		= IsDlgButtonChecked(hwndDlg,IDC_EVENT_ICQCHECKSTATUS);
	hs_events.EventUnknown				= IsDlgButtonChecked(hwndDlg,IDC_EVENT_UNKNOWN);
	hs_events.KeepHppBookMarks			= IsDlgButtonChecked(hwndDlg,IDC_EVENT_KEEPBOOKMARKS);

	DBWriteContactSettingByte(NULL,ModuleName,"EventsIncomingMessages", hs_events.EventsIncomingMessages);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsOutgouingMessages",hs_events.EventsOutgouingMessages);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsURL",hs_events.EventsURL);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsStatusMessage",hs_events.EventsStatusMessage);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsFiles",hs_events.EventsFiles);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsContacts",hs_events.EventsContacts);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsStatus",hs_events.EventsStatus);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsNick",hs_events.EventsNick);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsAvatar",hs_events.EventsAvatar);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsWATrack",hs_events.EventsWATrack);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsVoiceCall",hs_events.EventsVoiceCall);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsICQRemove",hs_events.EventsICQRemove);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsICQIgnoreCheck",hs_events.EventsICQIgnoreCheck);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsICQClientChange",hs_events.EventsICQClientChange);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsICQAuth",hs_events.EventsICQAuth);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsICQCheckStatus",hs_events.EventsICQCheckStatus);
	DBWriteContactSettingByte(NULL,ModuleName,"EventsUnknown",hs_events.EventUnknown);
	DBWriteContactSettingByte(NULL,ModuleName,"KeepHppBookmarks",hs_events.KeepHppBookMarks);

	ModifyMainMenu();
}

void SaveSettingsList(HWND hwndDlg)
{
	HWND listItem = GetDlgItem(hwndDlg, IDC_LIST);
	HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST,0,0);
	while ( hContact != NULL )
	{
		HANDLE hItem = (HANDLE)SendMessage(listItem, CLM_FINDCONTACT, (WPARAM)hContact, 0);
		
		if (SendMessage(listItem, CLM_GETCHECKMARK, (WPARAM)hItem, 0))
			DBWriteContactSettingByte(hContact,"CList","SweepHistory",1);			
		else
			DBDeleteContactSetting(hContact,"CList","SweepHistory");
		hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact,0);
	}
	
	DBWriteContactSettingByte(hContact,ModuleName,"SweepSystemHistory",
		(BYTE)SendMessage(listItem, CLM_GETCHECKMARK, (WPARAM)hItemSystem, 0));	

}
BOOL CALLBACK DlgProcGeneralOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_INITDIALOG:
			TranslateDialogDefault(hwndDlg);
			LoadSettingsGeneral(hwndDlg);
			return TRUE;

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
		case WM_COMMAND:
				case IDC_SELECT_ALL:
					SetAllNoneEventsCheckmarks(hwndDlg, BST_CHECKED);
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
					break;
				case IDC_SELECT_NONE:
					SetAllNoneEventsCheckmarks(hwndDlg, BST_UNCHECKED);
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
					break;
				case  IDC_STARTUPSWEEP  : 
					CheckDlgButton(hwndDlg,IDC_SHUTDOWNSWEEP,BST_UNCHECKED);
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
					break;
				case  IDC_SHUTDOWNSWEEP : 
					CheckDlgButton(hwndDlg,IDC_STARTUPSWEEP,BST_UNCHECKED);
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
					break;
				default:
					SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
			}

			return TRUE;

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->idFrom) 
			{
				case 0:
					switch (((LPNMHDR)lParam)->code)
					{
						case PSN_APPLY: // if "Apply" pressed then...						
							SaveSettingsGeneral(hwndDlg);
							return TRUE;
					}
					break;
			}
			break;
	}
	return FALSE;
}

BOOL CALLBACK DlgProcListOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_INITDIALOG:
			TranslateDialogDefault(hwndDlg);

			{	// Add "All contacts" and "System History" elements to contactlist
				CLCINFOITEM cii = {0};
				cii.cbSize = sizeof(cii);
				cii.flags = CLCIIF_GROUPFONT | CLCIIF_CHECKBOX;;
				cii.pszText = TranslateT("** All contacts **");
				hItemAll = (HANDLE)SendDlgItemMessage(hwndDlg,IDC_LIST,CLM_ADDINFOITEM,0,(LPARAM)&cii);

				cii.flags = CLCIIF_GROUPFONT | CLCIIF_CHECKBOX;;
				cii.pszText = TranslateT(">> System history **");
				hItemSystem = (HANDLE)SendDlgItemMessage(hwndDlg,IDC_LIST,CLM_ADDINFOITEM,0,(LPARAM)&cii);
			}

			SetWindowLong(GetDlgItem(hwndDlg,IDC_LIST),GWL_STYLE, GetWindowLong(GetDlgItem(hwndDlg,IDC_LIST),GWL_STYLE)|(CLS_CHECKBOXES|CLS_GROUPCHECKBOXES|CLS_SHOWHIDDEN));
			SendDlgItemMessage(hwndDlg,IDC_LIST,CLM_AUTOREBUILD,0,0);
			
			ResetListOptions(GetDlgItem(hwndDlg,IDC_LIST));

			return TRUE;

		case WM_SETFOCUS: 
			SetFocus(GetDlgItem(hwndDlg,IDC_LIST));

		case WM_COMMAND:
			SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); // Send message to activate "Apply" button
			return TRUE;

		case WM_NOTIFY:
			switch(((LPNMHDR)lParam)->idFrom) 
			{
			
			case IDC_LIST:
				switch (((LPNMHDR)lParam)->code)
					{
						case CLN_CONTACTMOVED:
						case CLN_NEWCONTACT:
						case CLN_LISTREBUILT:
							LoadSettingsList(hwndDlg);
							break;	

						case CLN_OPTIONSCHANGED : 
							ResetListOptions(GetDlgItem(hwndDlg,IDC_LIST));
							break;
						case CLN_CHECKCHANGED:
							{
								NMCLISTCONTROL *nm = (NMCLISTCONTROL*)lParam;
								HANDLE hContact;
								HANDLE hItem;
								
								if (bListInit) break;
								
								if (nm->flags&CLNF_ISINFO && (nm->hItem == hItemAll))
								{
									int check;
									
									HWND hClist = GetDlgItem(hwndDlg, IDC_LIST);
									check = SendMessage(hClist, CLM_GETCHECKMARK, (WPARAM)hItemAll, 0);

									hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST,0,0);
									while ( hContact != NULL )
									{
										hItem = (HANDLE)SendMessage(hClist, CLM_FINDCONTACT, (WPARAM)hContact, 0);
										
										if (hItem)
											SendMessage(hClist, CLM_SETCHECKMARK, (WPARAM)hItem, check);
										hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT,(WPARAM)hContact,0);
									}
									SendMessage(hClist, CLM_SETCHECKMARK, (WPARAM)hItemSystem, check);

								}
								else
								{
									HWND hClist = GetDlgItem(hwndDlg, IDC_LIST);
									UpdateAllContactsCheckmark(hClist, hItemAll);
								}
								SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
							}
							break;
				} 
				break;
				
				case 0:
					switch (((LPNMHDR)lParam)->code)
					{
						case PSN_APPLY: // if "Apply" pressed then...						
							SaveSettingsList(hwndDlg);
							return TRUE;
					}
					break;
			}
			break;
	}
	return FALSE;
}
// ------------------------------------------------------------------------------------------------------- 

void OptionsInit( void )
{
	hs_events.EventsIncomingMessages	= DBGetContactSettingByte(0,ModuleName,"EventsIncomingMessages",0);
	hs_events.EventsOutgouingMessages	= DBGetContactSettingByte(0,ModuleName,"EventsOutgouingMessages",0);
	hs_events.EventsURL					= DBGetContactSettingByte(0,ModuleName,"EventsURL",0);
	hs_events.EventsStatusMessage		= DBGetContactSettingByte(0,ModuleName,"EventsStatusMessage",0);
	hs_events.EventsFiles				= DBGetContactSettingByte(0,ModuleName,"EventsFiles",0);
	hs_events.EventsContacts			= DBGetContactSettingByte(0,ModuleName,"EventsContacts",0);
	hs_events.EventsStatus				= DBGetContactSettingByte(0,ModuleName,"EventsStatus",0);
	hs_events.EventsNick				= DBGetContactSettingByte(0,ModuleName,"EventsNick",0);
	hs_events.EventsAvatar				= DBGetContactSettingByte(0,ModuleName,"EventsAvatar",0);
	hs_events.EventsWATrack				= DBGetContactSettingByte(0,ModuleName,"EventsWATrack",0);
	hs_events.EventsVoiceCall			= DBGetContactSettingByte(0,ModuleName,"EventsVoiceCall",0);
	hs_events.EventsICQRemove			= DBGetContactSettingByte(0,ModuleName,"EventsICQRemove",0);
	hs_events.EventsICQIgnoreCheck		= DBGetContactSettingByte(0,ModuleName,"EventsICQIgnoreCheck",0);
	hs_events.EventsICQClientChange		= DBGetContactSettingByte(0,ModuleName,"EventsICQClientChange",0);
	hs_events.EventsICQAuth				= DBGetContactSettingByte(0,ModuleName,"EventsICQAuth",0);
	hs_events.EventsICQCheckStatus		= DBGetContactSettingByte(0,ModuleName,"EventsICQCheckStatus",0);
	hs_events.EventUnknown				= DBGetContactSettingByte(0,ModuleName,"EventsUnknown",0);
	hs_events.KeepHppBookMarks			= DBGetContactSettingByte(0,ModuleName,"KeepHppBookmarks",1);
}
// ------------------------------------------------------------------------------------------------------- 

int HSOptInitialise(WPARAM wParam,LPARAM lParam)
{
	OPTIONSDIALOGPAGE odp = {0};

	odp.cbSize = sizeof(odp);
	odp.hInstance = hInst;
	odp.pszGroup = Translate("History");
	odp.pszTab   = Translate("General");
	odp.pszTemplate = MAKEINTRESOURCE(IDD_OPT_HISTORYSWEEPER);
	odp.pszTitle = Translate(ModuleName);
	odp.pfnDlgProc = DlgProcGeneralOpts;
	odp.flags = ODPF_BOLDGROUPS;
	CallService(MS_OPT_ADDPAGE,wParam,(LPARAM)&odp);

	odp.pszTab   = Translate("Contacts");
	odp.pszTemplate = MAKEINTRESOURCE(IDD_OPT_CONTACTS);
	odp.pfnDlgProc = DlgProcListOpts;
	CallService(MS_OPT_ADDPAGE,wParam,(LPARAM)&odp);

	return 0;
}
